package org.swimv2.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneRicerca;
import org.swimv2.controller.GestioneRicercaRemote;
import org.swimv2.model.Persona;



public class ListaAmiciServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	ArrayList<String> content1 = new ArrayList<String>();
	ArrayList<String> checker1 = new ArrayList<String>();
	
	public ListaAmiciServlet() {
        super();
    }
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Persona u = (Persona) request.getSession().getAttribute("utente");
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneRicerca.JNDI_NAME);
			GestioneRicercaRemote manager = (GestioneRicercaRemote)PortableRemoteObject.narrow(obj,GestioneRicercaRemote.class);
			
			Collection<Persona> lstAmici= manager.getAmiciUtente(u.getUserID());
			request.setAttribute("lstAmici", lstAmici);
			request.getRequestDispatcher("WEB-INF/listaAmici.jsp").
			forward(request, response);			

		}
		catch(NamingException e){
			e.printStackTrace();
			
		}
		
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	

}
